<template>
  <div class="app-container">
    <data-grid
      url="/exam/question/mistake/list"
      data-name="listQuery"
      ref="dataList"
      @dataRest="onDataRest"
    >
      <template slot="form">
        <el-form-item label="科目">
          <el-select
            v-model="listQuery.subjectId"
            class="filter-item"
            placeholder="请选择..."
            @keyup.enter.native="handleFilter"
          >
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </template>
      <!--extendOperation-->
      <!--body-->
      <template slot="body">
        <el-table-column align="center" prop="id" label="ID" width="100px"></el-table-column>
        <el-table-column align="center" prop="subjectName" label="科目"></el-table-column>
        <el-table-column align="center" prop="titleContent" label="题目"></el-table-column>
        <el-table-column align="center" prop="correct" label="题目"></el-table-column>
        <el-table-column align="center" prop="wrongAnswer" label="错误答案"></el-table-column>

        <el-table-column label="创建时间">
          <template slot-scope="scope">
            <span>{{ scope.row.createdTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          width="230"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="danger" size="mini" @click="dropRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>
    </data-grid>
  </div>
</template>

<script>
import DataGrid from "@/components/DataGrid";
import { parseTime } from "@/utils";
import waves from "@/directive/waves";
import { deleteById } from "@/api/exam/questionMistake";
import { getSubjectList } from "@/api/exam/subject";
const statusOptions = [
  { key: "正常", value: "1" },
  { key: "删除", value: "0" }
];
// const erpFlagOptions=[{'key':'否','value':'0'},{'key':'是','value':'1'}]

const statusTypeKeyValue = statusOptions.reduce((acc, cur) => {
  acc[cur.value] = cur.key;
  return acc;
}, {});
// const erpFlagTypeKeyValue = erpFlagOptions.reduce((acc, cur) => {
//   acc[cur.value] = cur.key
//   return acc
// }, {})

export default {
  name: "mistakeRecord",
  components: { DataGrid },
  directives: { waves },
  filters: {
    statusFilter(status) {
      return statusTypeKeyValue[status];
    },
    statusTagFilter(status) {
      const statusMap = {
        "1": "success",
        "0": "danger"
      };
      return statusMap[status];
    },
    erpFlagFilter(flag) {
      return erpFlagTypeKeyValue[flag];
    },
    erpFlagTagFilter(flag) {
      const statusMap = {
        "1": "success",
        "0": "info"
      };
      return statusMap[flag];
    },
    parseTime
  },
  data() {
    return {
      tableKey: 0,
      total: 0,
      list: null,
      listLoading: true,
      listQuery: {
        pageNo: 1,
        limit: 10,
        subjectId: null
      },
      mistakeForm: {
        id: "",
        questionId: "",
        subjectId: "",
        subjectName: "",
        titleContent: "",
        wrongAnswer: "",
        createdTime: "",
        correct: ""
      },
      subjectList:[],
      statusOptions: statusOptions
    };
  },
  watch: {
    // roleVisible(val) {
    //   if(!val){
    //     this.defaultSelected = [];
    //   }
    // }
  },
  created() {
    this.getSubjectList();
  },

  methods: {
    onDataRest() {
      this.listQuery = {};
    },
    handleFilter() {
      this.$refs.dataList.fetchData();
    },
    getSubjectList() {
      getSubjectList().then(res => {
        if (res.data) {
          this.subjectList = res.data;
        } else {
          this.$message.error(res.code);
        }
      });
    },
    dropRow(row) {
      this.$confirm("您确定要删除该数据吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {};
          if (row.id) {
            drop(row.id).then(res => {
              this.$refs.dataList.fetchData();
            });
          }
        })
        .catch(() => {});
    }
  }
};
</script>
